package com.lin.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lin.reggie.Dto.DishDto;
import com.lin.reggie.common.CustomException;
import com.lin.reggie.entity.Dish;
import com.lin.reggie.entity.DishFlavor;
import com.lin.reggie.mapper.DishMapper;
import com.lin.reggie.service.DishFlavorService;
import com.lin.reggie.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author lin
 * @since 2022-05-26
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;

    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {

        //先保存基本信息
        this.save(dishDto);
        Long dishDtoId = dishDto.getId();
        List<DishFlavor> flavors =dishDto.getFlavors();
        flavors.stream().map((item) ->{
            item.setDishId(dishDtoId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据
        dishFlavorService.saveBatch(flavors);
//        LambdaQueryWrapper<> queryWrapper = new LambdaQueryWrapper<>();

    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询当前菜品的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        DishDto dishDtoupdate = new DishDto();
        //清理当前菜品对应的口味数据
        LambdaQueryWrapper<DishFlavor> dishDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishDtoLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishDtoLambdaQueryWrapper);
        //添加当前菜品对应的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long dishDtoId = dishDto.getId();
        flavors.stream().map((item) ->{
            item.setDishId(dishDtoId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据
        dishFlavorService.saveBatch(flavors);
        this.updateById(dishDto);
    }

    @Override
    public void removewithFlavors(List<Long> id) {
        //构造条件查询器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //先查询该菜品是否在售卖，如果是则抛出业务异常
        queryWrapper.in(id!=null,Dish::getId,id);
        List<Dish> list = this.list(queryWrapper);
        for (Dish dish : list) {
            Integer status = dish.getStatus();
            //如果不是在售卖,则可以删除
            if (status == 0){
                this.removeById(dish.getId());
            }else {
                //此时应该回滚,因为可能前面的删除了，但是后面的是正在售卖
                throw new CustomException("删除菜品中有正在售卖菜品,无法全部删除");
            }
        }
    }
}
